﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using System.Text;
using LeHouse.Server.WebApi.Common;
namespace LeHouse.Server.WebApi.Controllers
{
    public class PersonalController : ApiController
    {
        Collateral_PersonalService bll = new Collateral_PersonalService();
        /// <summary>
        /// 资产登记的数据添加
        /// </summary>
        /// <param name="s">ajax传过来的需要添加的数据</param>
        /// <returns></returns>       
        [HttpPost]
        [Route("api/Personal/PersonalOrBorrower")]
        public bool InsertOrUpdate([FromBody]PersonalOrBorrower parms)
        {
            parms.Collateral_Personal.T_State = Convert.ToInt32(EnumSelectState.waitProspect);
            parms.Collateral_Personal.T_Date = DateTime.Now;
            //如果项目号为空，是添加，获取新报告号
            if (string.IsNullOrEmpty(parms.Collateral_Personal.T_ReportNumber))
            {
                //获取New项目号
                string ReportNumber = bll.SortReportNumber();
                parms.Collateral_Personal.T_ReportNumber = ReportNumber;
                foreach (Collateral_Borrower cb in parms.Collateral_BorrowerList)
                {
                    cb.T_ReportNumber = ReportNumber;
                }
                parms.State = 1;
            }
            else
            {//否则修改

                Collateral_Personal cp = bll.Select(t => t.T_ReportNumber == parms.Collateral_Personal.T_ReportNumber).FirstOrDefault();
                if (cp == null)
                    return false;
                string ReportNumber = cp.T_ReportNumber;
                parms.Collateral_Personal.T_ReportNumber = ReportNumber;
                foreach (Collateral_Borrower cb in parms.Collateral_BorrowerList)
                {
                    cb.T_ReportNumber = ReportNumber;
                }
                parms.State = 2;
            }

            ////押物评估登记基本信息表添加and修改操作
            if (bll.PersonalOrBorrowerInsertOrUpdate(parms) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///查看资产登记表的所有数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Personal/SelectPersonalAll")]
        public List<Collateral_Personal> SelectPersonalAll([FromBody]string T_ReportNumber)
        {
            Collateral_PersonalService select = new Collateral_PersonalService();
            if (string.IsNullOrEmpty(T_ReportNumber))
            {
                return select.Select();
            }
            else
            {
                //2016-11-21 杨继平进行优化
                return select.Select(p => p.T_ReportNumber.Contains(T_ReportNumber.Trim()));
                //var d1 = select.Select().Where(p => p.T_ReportNumber.Contains(T_ReportNumber.Trim())).ToList();
            }

        }

        //  <summary>
        // 查看资产登记表的所有数据by  id
        //  </summary>
        //   <returns></returns>
        [HttpGet]
        [Route("api/Personal/selectbyP_ReportNumber")]
        public Jointshow selectbyid([FromUri] string P_ReportNumber)
        {
            //var v = Request.Content;
            Collateral_PersonalService select = new Collateral_PersonalService();
            return select.selectbyid(P_ReportNumber);
        }

        //  <summary>
        //  Wap查看根据谁登陆的去看他的单子
        //  </summary>
        //   <returns></returns>
        [HttpGet]
        [Route("api/Personal/WapShow")]
        public List<Collateral_Personal> WapShow([FromUri] string S_account)
        {
            System_StaffService service = new System_StaffService();
            System_Staff Staff = new System_Staff();
            Staff = service.Select().Where(p => p.T_Account == S_account).FirstOrDefault();
            List<Collateral_Personal> s = bll.Select().Where(p => p.T_SiteSurveyid == Staff.T_StaffID).ToList();
            return s;
        }
        //<summary>
        //接到WAP端传来的报告号约到
        //</summary>
        //<returns></returns>
        [HttpGet]
        [Route("api/Personal/UpdateP_ReportNumberyes")]
        public bool UpdateP_ReportNumber([FromUri] string P_ReportNumber, string P_cause)
        {
            Collateral_Personal Personal = new Collateral_Personal();
            Personal = bll.Select().Where(p => p.T_ReportNumber == P_ReportNumber).FirstOrDefault();
            Personal.T_State = 1;
            Personal.T_Cause = P_cause;
            if (bll.Update(Personal) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据报告号去查约没约到加载显示
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Personal/selectbyP_ReportNumberss")]
        public Collateral_Personal selectbyP_ReportNumberss(string P_ReportNumber)
        {
            return bll.selectbyP_ReportNumberss(P_ReportNumber);
        }
        //<summary>
        //接到WAP端传来的报告号没约到
        //</summary>
        //<returns></returns>
        [HttpGet]
        [Route("api/Personal/UpdateP_ReportNumberNO")]
        public bool UpdateP_ReportNumberNO([FromUri] string P_ReportNumber, string P_cause)
        {
            Collateral_Personal Personal = new Collateral_Personal();
            Personal = bll.Select().Where(p => p.T_ReportNumber == P_ReportNumber).FirstOrDefault();
            Personal.T_State = 0;
            Personal.T_Cause = P_cause;
            if (bll.Update(Personal) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// WAP 端请求模糊查询地址
        /// </summary>
        /// <param name="P_located"></param>
        /// <returns></returns> 
        [HttpGet]
        [Route("api/Personal/searchbyP_located")]
        public List<Collateral_Personal> searchbyP_located([FromUri] string C_project)
        {
            return bll.searchbyP_located(C_project);
        }
        //<summary>
        //接到WAP端传来的报告号
        //</summary>
        //<returns></returns>
        //[HttpGet]
        //[Route("api/Personal/returnradio")]
        //public t_Personal returnradio([FromUri] string P_ReportNumber)
        //{
        //    t_Personal Personal = new t_Personal();
        //    Personal = bll.Select().Where(p => p.P_ReportNumber == P_ReportNumber).FirstOrDefault();
        //}

        /// <summary>
        /// 房产抵押基本信息-删除-未预约勘查信息之前可以删除
        /// </summary>
        /// <param name="T_ReportNumber"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Personal/delete")]
        public bool Personal_delete([FromBody]string T_ReportNumber)
        {
            var d1 = bll.Select().Where(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
            if (d1 == null)
            {
                return false;
            }
            return bll.DeleteById(d1.T_PersonalID);
        }

    }
}
